//
//  SpeedDialInserterViewController.m
//  LabanBrowser
//
//  Created by -Xh- on 3/22/13.
//  Copyright (c) 2013 VNG. All rights reserved.
//

#import "SpeedDialInserterViewController.h"

@interface SpeedDialInserterViewController ()

@end

@implementation SpeedDialInserterViewController{
    NSMutableArray* historyArray;
    HistoryModel* currentSelectedHistory;
}
@synthesize delegate;

-(void)relocalize{
    
    [self.topTitleLB setText:[MyUtilities getLocalizeStringFromTag:@"AddSpeedDialTitle"]];
    self.titleTF.placeholder = [MyUtilities getLocalizeStringFromTag:@"Title"];
    self.urlTF.placeholder= [MyUtilities getLocalizeStringFromTag:@"URL"];
    self.topTitleLB.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7f];
    self.topTitleLB.shadowOffset = CGSizeMake(1.0, 2.0);
    
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        historyArray=[[NSMutableArray alloc]init];
        
        
    }
    
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleTF.delegate=self;
    self.urlTF.delegate=self;
    //self.historyTBV.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.topBackgroundIMV.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.titleTF.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.urlTF.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.topTitleLB.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    self.cancelBT.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
    self.saveBT.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated{
    self.titleTF.text=self.urlTF.text=@"";
    historyArray=[[[StaticVariables shareVariables]historyAccess] getAllRecordsExceptSpeedDial];
    [self relocalize];
    [self.historyTBV performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}

#pragma mark uitableView
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"HistoryCellReuse";
    HistoryCell *cell;
    
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell=(HistoryCell*)[[[NSBundle mainBundle]loadNibNamed:@"HistoryCell" owner:self options:nil] objectAtIndex:0];
        [cell setUpStyle];
        cell.starButton.hidden=YES;
        
    }
    HistoryModel* history=[historyArray objectAtIndex:indexPath.row];
    cell.titleLabel.text=history.title;
    cell.urlLabel.text=history.url;

    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return historyArray.count;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [MyUtilities getLocalizeStringFromTag:@"PickURL"];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    currentSelectedHistory=[historyArray objectAtIndex:indexPath.row];
    self.titleTF.text=currentSelectedHistory.title;
    self.urlTF.text=currentSelectedHistory.url;

}
- (IBAction)saveSpeedDial:(id)sender {
    if(currentSelectedHistory!=nil){
        SpeedDialModel* speedDial=[[SpeedDialModel alloc]initWithTitle:self.titleTF.text AndURL:currentSelectedHistory.url];
        //speedDial.
        [[[StaticVariables shareVariables]speedDialAccess] insert:speedDial];
        
        //speed
        if(delegate && [delegate respondsToSelector:@selector(onSavingSpeedDialToDB:)]){
            [delegate onSavingSpeedDialToDB:speedDial];
            
        }
        currentSelectedHistory = nil;
    
    }else{
        [[[UIAlertView alloc]initWithTitle:[MyUtilities getLocalizeStringFromTag:@"AlertWarningTitle"]
                                   message:[MyUtilities getLocalizeStringFromTag:@"AddSpeedDialWarningMessage"]
                                  delegate:nil
                         cancelButtonTitle:[MyUtilities getLocalizeStringFromTag:@"ActionCancel"]
                         otherButtonTitles: nil]show];
    }
    
}
- (IBAction)backToMainVC:(id)sender {
    if(delegate && [delegate respondsToSelector:@selector(onCancelingSpeedDialInserter)]){
        [delegate onCancelingSpeedDialInserter];
    }
    currentSelectedHistory = nil;
}

#pragma mark textfieldProtocol
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    [textField selectAll:textField];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (void)viewDidUnload {
    [self setTopBackgroundIMV:nil];
    [self setTopTitleLB:nil];
    [super viewDidUnload];
}
@end
